package com.dlut.exer;

import com.dlut3.util.JDBCUtil;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;

public class Exer1Test {

    @Test
    public void test(){
        Scanner scan = new Scanner(System.in);
        System.out.println("输入用户名：");
        String name = scan.next();
        System.out.println("输入邮箱：");
        String email = scan.next();
        System.out.println("输入生日：");
        String birthday = scan.next();//'1992-01-01'

        String sql = "insert into customers(name,email,birth) values(?,?,?)";
        int insertCount = update(sql, name, email, birthday);
        if (insertCount > 0)
            System.out.println("操作成功！");
        else
            System.out.println("添加失败！");
    }
    //通用的增删改操作
    public int update(String sql, Object ...args){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            //1.获取数据库的链接
            conn = JDBCUtil.getConnection();
            //2.预编译sql语句
            ps = conn.prepareStatement(sql);
            //3.填充占位符
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i+1, args[i]);
            }
            //4.执行操作
//            ps.execute();
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //5.资源的关闭
            JDBCUtil.closeResource(conn,ps);
        }
        return 0;

    }
}
